package com.cheer.dao;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.cheer.dto.OaLearnNumberDTO;
import com.cheer.entity.OaLearnEntryEntity;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.cheer.entity.OaUserEntity;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.springframework.stereotype.Repository;

import java.util.List;

/**
 * 报名人员
 *
 * @author cheer
 */
@Mapper
@Repository
public interface OaLearnEntryDao extends BaseMapper<OaLearnEntryEntity> {

    default List<OaLearnEntryEntity> selectListByOpenid(String openid) {
        return selectList(new QueryWrapper<OaLearnEntryEntity>()
                .eq("openid", openid)
                .orderByDesc("add_time"));
    }

    List<OaLearnNumberDTO> selectLearnNumberByLearn(@Param("learnIds") List<Integer> learnIds);

    Integer selectLearnNumberByPath(@Param("learnId") Integer learnId, @Param("path") String path);

    Integer selectLearnNumberLimitTimeByPath(@Param("learnId") Integer learnId,
                                    @Param("path") String path,
                                    @Param("start") String start,
                                    @Param("end") String end);


    default IPage<OaLearnEntryEntity> selectPageByPath(Integer pageNum, Integer pageSize, Integer learnId, String path) {
        return selectPage(new Page<>(pageNum, pageSize), new QueryWrapper<OaLearnEntryEntity>()
                .eq("learn_id", learnId)
                .likeRight("path", path));
    }

    List<OaLearnNumberDTO> selectLearnNumberByPathGroup(@Param("learnIds") List<Integer> learnIds, @Param("path") String path);

    default Integer selectCountByLearnId(Integer learnId) {
        return selectCount(new QueryWrapper<OaLearnEntryEntity>()
                .eq("learn_id", learnId));
    }

    default OaLearnEntryEntity selectOneByOpenid(String openid, Integer learnId) {
        return selectOne(new QueryWrapper<OaLearnEntryEntity>()
                .eq("openid", openid)
                .eq("learn_id", learnId)
                .last("limit 1"));
    }

    default void updatePathByDepartment(Integer department, String path) {
        OaLearnEntryEntity learnEntry = new OaLearnEntryEntity()
                .setPath(path);
        update(learnEntry, new QueryWrapper<OaLearnEntryEntity>()
                .eq("department", department));
    }

    default void updateByOpenid(OaLearnEntryEntity oaLearnEntryEntity) {
        update(oaLearnEntryEntity, new QueryWrapper<OaLearnEntryEntity>()
            .eq("openid", oaLearnEntryEntity.getOpenid()));
    }

    default List<OaLearnEntryEntity> selectListByPath(Integer learnId, String path) {
        return selectList(new QueryWrapper<OaLearnEntryEntity>()
                .eq("learn_id", learnId)
                .likeRight("path", path));
    }
}
